import { ref } from 'vue'
import { uploadFile } from '@/utils/request'
import { message } from './useToast'

export const Max_FILE = 1
const MAX_SIZE = 10  // MB


export const useUploadFile = () => {

  const fileList = ref([])

  const afterRead = async ({ file, index, name }) => {
    if (file.size > MAX_SIZE * 1024 * 1024) {
      message('文件不能大于' + MAX_SIZE + 'MB')
      return
    }
    console.log('afterRead', file)
    const url = await uploadFile(file.url)
    console.log('res', url)
    fileList.value.push({
      url
    })
  }
  const deleteFn = (event) => {
    fileList.value.splice(event.index, 1);
  }
  const initData = (data) => {
    fileList.value = data
  }

  return {
    fileList,
    afterRead,
    deleteFn,
    initData
  }
}